package com.ltmonitor.web.controller.command;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.entity.TerminalParam;
import com.ltmonitor.service.IBasicDataService;
import com.ltmonitor.service.ITerminalCommandService;
import com.ltmonitor.service.ITerminalParamService;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.util.StringUtil;
import com.ltmonitor.web.util.JsonMessage;
/**
 * 终端参数配置和查询
 * @author DELL
 *
 */
@Controller
@RequestMapping("/terminalParam")
public class TerminalParamAction extends TerminalCommandAction {

	@Autowired
	protected ITerminalParamService terminalParamService;

	/**
	 * 
	 * @param vehicleId
	 * @param paramId 要查询或者修改的参数Id列表
	 * @param operation
	 * @param paramValue 要修改的参数值
	 * @param allSelectedVehicle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/send.action")
	public JsonMessage  execute(long vehicleId,String[] paramId,String operation,String[] paramValue,String allSelectedVehicle) {
		if ("modify".equals(operation)
				&& (paramId == null || paramId.length == 0))
			return json(false, "请选择要修改的参数！");
		int m = 0;
		try {
			
			
			StringBuilder cmdData = new StringBuilder();
			if (paramId != null) {
				for (String id : paramId) {
					int index = id.indexOf("0x");
					if (index >= 0)
						id = id.substring(index + 2);
					int intValue = Integer.valueOf(id, 16);
					cmdData.append(intValue);
					if ("modify".equals(operation)) {
						cmdData.append(",").append(paramValue[m]);	
					}
						
					cmdData.append(';');
					m++;
				}
			}
			TerminalCommand tc = new TerminalCommand();
			tc.setVehicleId(vehicleId);
			if ("modify".equals(operation))
			{
				tc.setCmdType(JT808Constants.CMD_CONFIG_PARAM);
				if("on".equals(allSelectedVehicle))
				{
					String REGISTER_KEY = "registerVehicleIds";
					List<Long> vehicleIdList = (List<Long>) super.getFromSession(REGISTER_VEHICLEID_KEY);
					if (vehicleIdList != null && vehicleIdList.size() > 1) {
						TerminalCommand command = new TerminalCommand();
						command.setCmdData(cmdData.toString());
						command.setCmdType(JT808Constants.CMD_CONFIG_PARAM);
						for (long vId : vehicleIdList)
						{
							if(vId != vehicleId)
							SendCommand(command);
						}
					}
				}
				
			}
			else
				tc.setCmdType(paramId == null ? JT808Constants.CMD_QUERY_PARAM : JT808Constants.CMD_QUERY_SPECIAL_PARAM);
			tc.setCmdData(cmdData.toString());
			SendCommand(tc);
			
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}

	}
	/**
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryTerminalParam.action")
	public JsonMessage queryTerminalParam(String operation, long commandId) {

		TerminalCommand tc = getTerminalCommand(commandId);
		BasicData bd = getBasicDataService().getBasicDataByCode(tc.getStatus(),
				"TCommandStatus");
		String status = bd != null ? bd.getName() : "";

		if (TerminalCommand.STATUS_SUCCESS.equals(tc.getStatus()) == false) {
			if (TerminalCommand.STATUS_NEW.equals(tc.getStatus()) == false
					&& TerminalCommand.STATUS_PROCESSING.equals(tc.getStatus()) == false) {
				return json(true, status);
			}
			return json(false, status);
		}
		//如果是修改参数，则不返回查询结果，只有是查询参数，才返回查询结果
		if ("modify".equals(operation)) {
			return json(true, status);
		}
		List<TerminalParam> result = this.terminalParamService.getTerminalParamList(commandId);
		if (result.size() == 0)
			return json(false, "");
		return json(true, result);
	}

	@RequestMapping("/viewOverSpeedConfig.action")
	public String viewOverSpeedConfig(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/command/overSpeedConfig";
	}
	

	@ResponseBody
	@RequestMapping("/overSpeedConfig.action")
	public JsonMessage  overSpeedConfig(long vehicleId,String[] paramId,String operation,String[] paramValue,String allSelectedVehicle) {
		return this.execute(vehicleId, paramId, operation, paramValue, allSelectedVehicle);
	}

	@RequestMapping("/viewTiredConfig.action")
	public String viewTiredConfig(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/command/tiredConfig";
	}
	@ResponseBody
	@RequestMapping("/tiredConfig.action")
	public JsonMessage  tiredConfig(long vehicleId,String[] paramId,String operation,String[] paramValue,String allSelectedVehicle) {
		return this.execute(vehicleId, paramId, operation, paramValue, allSelectedVehicle);
	}

	

	@RequestMapping("/view.action")
	public String getTemplate(long vehicleId, Model m,String paramType) {
		if (paramType == null)
			paramType = "common";

		List<BasicData> paramTemplateData = new ArrayList<BasicData>();
		List<BasicData> result = basicDataService
				.getBasicDataByParentCode("TerminalParam");
		for (BasicData bd : result) {
			if (StringUtil.isNullOrEmpty(paramType) == false
					&& paramType.equals(bd.getCode()) == false)
				continue;
			paramTemplateData.add(bd);
		}
		m.addAttribute("paramTemplateData", paramTemplateData);
		m.addAttribute("vehicleId",vehicleId);
		m.addAttribute("paramType",paramType);
		return "/command/terminalParam";
	}




}
